package com.iamgod.savemoney.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DateUtils {
	
	/**
	 * ʱ����ַ�ת�����ַ�
	 * @param timestamp ʱ���Ϊ10λ��php��� ����13λ��java��ɣ�
	 * @return
	 */
	public static String timestampStrToDateStr(String timestamp){
		try{
		switch(timestamp.length()){
		case 10:
			return new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Long.valueOf(timestamp)*1000);
		case 13:
			return new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Long.valueOf(timestamp));
		}
		}catch(Exception e){
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * �����ַ�תʱ����ַ�
	 * @param timestamp ʱ���Ϊ10λ��php��� ����13λ��java��ɣ�
	 * @return
	 */
	public static String dateStrToTimestamp(String datetime){
		SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
		if(datetime!=null){
			switch(datetime.length()){
			case 10:
				datetime = datetime+" 00:00:00";
				try {
					return (String.valueOf((sdf.parse(datetime)).getTime()/1000));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			case 19:
				try {
					return (String.valueOf((sdf.parse(datetime)).getTime()/1000));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return "";
	}
	
	/**
	 * ��Calendar����ת��Ϊ��ݿ���յĸ�ʽ��ʱ���ַ�0000-00-00
	 * @param cd1 Calendar��ʽ������
	 * @return ��ʽ���������ַ�
	 */
	public static String CalToString(Calendar cd1){
		String date1 = (new StringBuilder().append(cd1.get(Calendar.YEAR)).append("-").append(
				(cd1.get(Calendar.MONTH) + 1) < 10 ? "0" + cd1.get(cd1.get(Calendar.MONTH) + 1) : (cd1.get(Calendar.MONTH) + 1)).append("-").append(
						(cd1.get(Calendar.DAY_OF_MONTH) < 10) ? "0" + cd1.get(Calendar.DAY_OF_MONTH) : cd1.get(Calendar.DAY_OF_MONTH))).toString();
		return date1;
	}
	

}
